//
//  XMGCommentCell.m
//  百思不得姐
//
//  Created by Mine on 16/8/19.
//  Copyright © 2016年 Min. All rights reserved.

/**
 1.添加menu
 2.在 -awakeFromNib中创建一次性的设置
 3.-setFrame 的调用
 4.经过测试，自定义控件自己类中一句代码都不写的话。同样可以创建成功，添加方法的目的是为了，在对应的地方添加自己的代码 
 */

#import "XMGCommentCell.h"
#import "XMGComment.h"
#import "UIImageView+WebCache.h"
#import "XMGUser.h"

@interface XMGCommentCell()
@property (weak, nonatomic) IBOutlet UIImageView    *profileImageView;
@property (weak, nonatomic) IBOutlet UIImageView    *sexView;
@property (weak, nonatomic) IBOutlet UILabel        *contentLabel;
@property (weak, nonatomic) IBOutlet UILabel        *usernameLabel;
@property (weak, nonatomic) IBOutlet UILabel        *likeCountLabel;
@property (weak, nonatomic) IBOutlet UIButton       *voiceButton;
@end

@implementation XMGCommentCell
// 添加menu的条件
// 1.
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
// 2.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

// 设置背景的Image
- (void)awakeFromNib
{
    UIImageView * bgView = [[UIImageView alloc]init];
    bgView.image = [UIImage imageNamed:@"mainCellBackground"];
    self.backgroundView = bgView;
}

- (void)setComment:(XMGComment *)comment
{
    _comment = comment;
    
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:comment.user.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.sexView.image = [comment.user.sex isEqualToString:XMGUserSexMale] ? [UIImage imageNamed:@"Profile_manIcon"] : [UIImage imageNamed:@"Profile_womanIcon"];
    self.contentLabel.text = comment.content;
    self.usernameLabel.text = comment.user.username;
    self.likeCountLabel.text = [NSString stringWithFormat:@"%zd", comment.like_count];
    
    if (comment.voiceuri.length) {
        self.voiceButton.hidden = NO;
        [self.voiceButton setTitle:[NSString stringWithFormat:@"%zd''", comment.voicetime] forState:UIControlStateNormal];
    } else {
        self.voiceButton.hidden = YES;
    }
}

- (void)setFrame:(CGRect)frame
{
#warning 这样修改后，cell左右都会留出margin的空间
//  一般在super frame:之前调用修改尺寸，会永久改变。外面再怎么设置frame都不会起作用
    frame.origin.x = XMGTopicCellMargin;
    frame.size.width -= 2*XMGTopicCellMargin;
    
    [super setFrame:frame];
}



@end
